package com.wanmait.baohan.controller.CustomerController;

import com.wanmait.baohan.pojo.*;
import com.wanmait.baohan.service.*;

import com.wanmait.baohan.vo.DataVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;

//客户信息管理
@Controller
@RequestMapping("customerInformation")
public class SupplyController {

    @Resource
    private SuppilerService suppilerService;
    @Resource
    private ShareHolderService shareHolderService;
    @Resource
    private ProductService productService;
    @Resource
    private BusinessContactService businessContactService;
    @Resource
    private DemandService demandService;
    @Resource
    private AgentService agentService;
    @Resource
    private AccountService accountService;
    //单个客户公司的详情数据（）
    @RequestMapping("detailed")
    public ModelAndView customerDatailed(String customerId,HttpServletRequest request)
    {
        //request.getSession().setAttribute("customerId",customerId);
       /* request.getSession().setAttribute("");*/
        ModelAndView modelAndView = new ModelAndView();
        Supplier supplier = (Supplier)suppilerService.findById(Integer.parseInt(customerId));
        List<Shareholder> shareholders = shareHolderService.findShareHolderById(Integer.parseInt(customerId));
        List<Product> products = productService.findAllProductsForCustomer(Integer.parseInt(customerId));
        List<BusinessContact> businessContacts = businessContactService.selectAllBusinessContactBy(Integer.parseInt(customerId));
        List<Demand> demands = demandService.findAllDemandBySupplier(Integer.parseInt(customerId));
        List<Agent> agents = agentService.selectAllForCustomerId(Integer.parseInt(customerId));
        List<Account> accounts = accountService.findAllAccountForCustomer(Integer.parseInt(customerId));
        modelAndView.addObject("supplier",supplier);
        modelAndView.addObject("shareholders",shareholders);
        modelAndView.addObject("products",products);
        modelAndView.addObject("businessContacts",businessContacts);
        modelAndView.addObject("demands",demands);
        modelAndView.addObject("agents",agents);
        modelAndView.addObject("accounts",accounts);
        return modelAndView;
    }
    //产品指标
    @RequestMapping("/productIndicators")
    public ModelAndView detailedData(String id,String customerId)
    {
       /* System.out.println(id);
        System.out.println(customerId);
        System.out.println("我要传过来了");*/
        ModelAndView modelAndView = new ModelAndView();
       /* List<ChemicalIndex> chemicalIndexList = productService.findAllProductsForCustomer(Integer.parseInt(customerId)).
                get(Integer.parseInt(id)-1).getChemicalIndexList();
        List<PhysicalIndex> physicalIndexList = productService.findAllProductsForCustomer(Integer.parseInt(customerId)).
                get(Integer.parseInt(id)-1).getPhysicalIndexList();
        modelAndView.addObject("chemicalIndexList",chemicalIndexList);
        modelAndView.addObject("physicalIndexList",physicalIndexList);*/
       Product product = productService.findOneProductsForCustomer(Integer.parseInt(customerId),Integer.parseInt(id));
       modelAndView.addObject("product",product);
       modelAndView.addObject("id",id);
       modelAndView.addObject("customerId",customerId);
        return modelAndView;
    }


    //客户股东信息登记
    @RequestMapping("/customerList")
    public String customerList()
    {
        return "customerInformation/customerList";
    }



    //所有客户信息详情
    @RequestMapping("/customerData")
    @ResponseBody
    public DataVo<Supplier> customerData(String supplierName)
    {
        Supplier supplier = new Supplier();
        supplier.setName(supplierName);
        List<Supplier> suppliers = suppilerService.selectAllot(supplier);
        DataVo<Supplier> dataVo = new  DataVo();
        dataVo.setCount((long)suppliers.size());
        dataVo.setData(suppliers);
       /* List<Dictionary> dictionaryList = suppilerService.select(13);
        modelAndView.addObject("dictionaryList",dictionaryList);*/
        return dataVo;
    }
    //股东信息录入
    @GetMapping("/shareholder")
    public ModelAndView addShareHolderInformation(HttpServletRequest request,String id)
    {
        ModelAndView modelAndView = new ModelAndView();
        List<Dictionary> dictionaryList = suppilerService.select(13);
        modelAndView.addObject("dictionaryList",dictionaryList);
        /*System.out.println(request.getSession().getAttribute("customerId"));*/
        modelAndView.addObject("customerId",id);
        return modelAndView;
    }
    @PostMapping("/shareholder")
    public void addShareHolderInformation(Shareholder shareholder) {
        System.out.println(shareholder.getSupplier().getId());
        System.out.println(shareholder.getName());
        System.out.println(shareholder.getDictionary().getId());
        /*Supplier supplier = new Supplier();
        supplier.setId();
        shareholder.setSupplier(supplier);*/
        shareHolderService.insert(shareholder);
    }
    //客户银行账号信息登记
    @GetMapping("/accountInformation")
    public ModelAndView addAccountInformation()
    {
        ModelAndView modelAndView = new ModelAndView();
        return modelAndView;
    }
    @PostMapping("/accountInformation")
    @ResponseBody
    public void addAccountInformation(MultipartFile file)
    {
        /*System.out.println(supplier.getName());
        System.out.println(supplier.getNatureOfBusiness());
        System.out.println(supplier.getCreateTime());
        System.out.println(supplier.getDictionaryIdCurrency());*/
       /* System.out.println(file.getOriginalFilename());
        *//*supplier.setBusinessLicense(file.getOriginalFilename());*//*
        suppilerService.insertAll(supplier);
        System.out.println("111");*/
    }
    @GetMapping("/enterpriseBasicInformation")
    //打开的添加供应商信息页面
    public ModelAndView addSupllyInformation()
    {
      ModelAndView modelAndView = new ModelAndView();
      List<Dictionary> dictionaryList = suppilerService.select(1);
      List<Dictionary> dictionaryList1 = suppilerService.select(2);
      List<Dictionary> dictionaryList2 = suppilerService.select(3);
        List<Dictionary> dictionaryList3 = suppilerService.select(19);

      modelAndView.addObject("dictionaryList",dictionaryList);
      modelAndView.addObject("dictionaryList1",dictionaryList1);
      modelAndView.addObject("dictionaryList2",dictionaryList2);
        modelAndView.addObject("dictionaryList3",dictionaryList3);

        return modelAndView;
    }
  /*  @PostMapping("enterpriseBasicInformation")
    @ResponseBody*/
    //打开的添加供应商信息页面
    /*public void addSupllyInformation(String name, String nature_of_business, MultipartFile file, String abbreviation, String duty_paragraph, String dictionary_id_type,
                                     String address, String legal_representative, String registered_amount, String dictionary_id_currency, String create_time,
                                     String end_time, String registration_authority, String dictionary_id_ownership, String photo, HttpServletRequest request)
    {
        System.out.println(name);
        System.out.println(nature_of_business);
        System.out.println(file.getOriginalFilename());
    }*/
  @PostMapping("enterpriseBasicInformation")
    public String addSupllyInformation(Supplier supplier ,MultipartFile file)
    {
        /*System.out.println(supplier.getName());
        System.out.println(supplier.getAbbreviation());
        System.out.println(supplier.getNatureOfBusiness());
        System.out.println(supplier.getCreateTime());
        System.out.println(supplier.getDictionaryA().getValue());
        System.out.println(file.getOriginalFilename());
*/
        /*supplier.setBusinessLicense(file.getOriginalFilename());*/
        //System.out.println(supplier.getDictionaryA().getId());
        //System.out.println(file.getOriginalFilename());
        supplier.setBusinessLicense(file.getOriginalFilename());
        suppilerService.insertAll(supplier);
        //System.out.println("111");
        return "redirect:/customerInformation/customerList";
    }
}

